package com.xiaoming.controller;

import com.xiaoming.base.ResponseBase;
import com.xiaoming.constants.Constants;
import com.xiaoming.feign.UserServiceFegin;
import com.xiaoming.utils.CookieUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;

/**
 * @author xiaoming
 * @Date 2020/3/12
 */
@Controller
public class IndexController {

    @Autowired
    private UserServiceFegin userServiceFegin;

    @RequestMapping("/index")
    public String index(HttpServletRequest request){
        String token = CookieUtil.getUid(request, Constants.COOKIE_MEMBER_TOKEN);
        if (StringUtils.isEmpty(token)){
            return "login";
        }
        ResponseBase responseBase = userServiceFegin.findUserByToken(token);
        if (!responseBase.getCode().equals(Constants.HTTP_RES_CODE_200)) {
            request.setAttribute("error", "请先登录!");
            return "login";
        }
        LinkedHashMap linkedHashMap = (LinkedHashMap) responseBase.getData();
        String username = (String) linkedHashMap.get("username");
        request.setAttribute("username", username);
        return "index";
    }
}
